package com.bankLogisticsSys.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import com.bankLogisticsSys.interceptor.ManageLoginInterceptor;
import com.bankLogisticsSys.model.Networkinfo;
import com.bankLogisticsSys.service.NetWorkService;
import com.bankLogisticsSys.service.PathService;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
@Before(ManageLoginInterceptor.class)
public class ManageNetWorkController extends Controller{
	NetWorkService service = new NetWorkService();
	PathService pservice = new PathService();
	
	public void list(){
		render("/WEB-INF/content/network/list.html");
	}
	public void list2(){
		render("/WEB-INF/content/network/list2.html");
	}
	
	public void jsonlist(){
		String type = getPara("type");
		String sort = getPara("sort","id");
		String order = getPara("order","asc");
		Page<Networkinfo> pageList = service.getList(getPara("name"), type,sort, order, getParaToInt("page"), getParaToInt("rows"));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", service.getCount(getPara("name"),type)); 
		map.put("rows", pageList.getList());
		renderJson(map);
	}

	public void add(){
		render("/WEB-INF/content/network/add.html");
	}
	public void modify(){
		int id = getParaToInt("networkinfo.id", 1);
		Networkinfo network = service.getNetwork(id);
		setAttr("networkinfo", network);
		render("/WEB-INF/content/network/modify.html");
	}
	
	public void superNetworkList(){
		List<Networkinfo> list = service.getSuperNetworkList();
		renderJson(list);
	}
	
	public void insert(){
		Networkinfo network = getModel(Networkinfo.class);
		if(!service.isExistNetworkCode(network.getNetworkCode())){
			network.setState(0);
			service.insert(network);
			render("/WEB-INF/content/network/list.html");
		}else{
			setAttr("errMsg", "网点编号已存在");
			render("/WEB-INF/content/network/add.html");
		}
	}
	
	public void update(){
		Networkinfo network = getModel(Networkinfo.class);
		service.edite(network);
		render("/WEB-INF/content/network/list.html");
	}
	
	/*
	 * edit path function
	 */
	public void editPath(){
		int source = getParaToInt("networkid");
		System.out.println(source);
		if(service.isSource(source)){
			Networkinfo net = Networkinfo.dao.findById(source);
			System.out.println(net);
			setAttr("networkinfo", net);
			render("/WEB-INF/content/path/setPath.html");
		}else{
			render("/WEB-INF/content/network/list.html");
		}
	}
	
	public void delete(){
		Networkinfo network = getModel(Networkinfo.class);
		Map<String, Integer> map = new HashMap<String, Integer>();
		if(service.delete(network)){
			map.put("result", 0);
		}else{
			map.put("result", 1);
		}
		renderJson(map);
	}
	
	public void index(){
		render("/WEB-INF/content/path/setPath.html");
	}
	
	public void bankNetworkList(){
		List<Networkinfo> list = pservice.getNetworkList("1");
		renderJson(list);
	}
	
	public void bankNetworkListMain(){
		List<Networkinfo> list = pservice.getNetworkList("2");
		renderJson(list);
	}
	
	public void emsNetworkList(){
		List<Networkinfo> list = pservice.getNetworkList("3");
		renderJson(list);
	}
	
	public void setPath(){
		String pathstr = getPara("pathstr");
		 StringTokenizer st = new StringTokenizer(pathstr, "-");
		 int sequence = 1;
		 int source = Integer.parseInt(st.nextToken());
		 if(pservice.hasPath(source)){
			 pservice.delPath(source);
		 }
		 pservice.savePath(source, source, sequence);
		 while(st.hasMoreElements()){  
			 sequence++;
			 int id = Integer.parseInt(st.nextToken());
			 pservice.savePath(source, id, sequence);
		 }
		 render("/WEB-INF/content/network/list.html");
	}
	public void getPath(){
		List<List<Object>> pathlist = pservice.getPathList();
		setAttr("pathlist", pathlist);
		render("/WEB-INF/content/path/list.html");
	}
}
